#include <bits/stdc++.h>
using namespace std;
#define ll long long
ll f[3005][3005];
int m,n,v=1;
ll a[3005],b[3005];
int num[3005];
int main(){
	a[0]=b[0]=-1-(1<<31);
	cin>>m;
	for(int i=1;i<=m;++i)cin>>a[i];
	cin>>n;
	for(int i=1;i<=n;++i)cin>>b[i];
	for(int i=1;i<=m;++i){
		ll k=0;
		if(b[0]<a[i])k=f[i-1][0];
		for(int j=1;j<=n;++j){
			if(a[i]==b[j])f[i][j]=k+1;
			else f[i][j]=f[i-1][j];
			if(f[i][j]==v&&a[i]==b[j])num[v++]=a[i];
			if(b[j]<a[i])k=max(k,f[i-1][j]);
		}
	}
	cout<<v-1<<endl;
	for(int i=1;i<v-1;++i)
		cout<<num[i]<<' ';
	cout<<num[v-1]<<endl;
	return 0;
}
